package com.example.zhangxin.comevedio.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;

import com.example.zhangxin.comevedio.R;
import com.example.zhangxin.comevedio.activity.activity.BaseActivity;
import com.example.zhangxin.comevedio.activity.adapter.FlashAdapter;
import com.example.zhangxin.comevedio.activity.bean.Version;
import com.example.zhangxin.comevedio.activity.utils.ShareprefreceUtils;
import com.example.zhangxin.comevedio.activity.utils.VersionUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements Animation.AnimationListener {


    private ImageView iv_flash;
    private ViewPager vp_flash;
    private AlphaAnimation animation;

    @Override
    public int getLayout() {
        return R.layout.activity_main;

    }

    @Override
    public void findView() {
        iv_flash = (ImageView) findViewById(R.id.iv_flash);
        vp_flash = (ViewPager) findViewById(R.id.vp_flash);
    }

    @Override
    public void setData() {
        List<Integer> data = new ArrayList<>();
        boolean frist = ShareprefreceUtils.read("frist", true);
        animation = new AlphaAnimation(0.5f, 1.0f);
        animation.setDuration(1000);
        iv_flash.setAnimation(animation);
        if(frist){
            data.add(R.mipmap.guide_bg0);
            data.add(R.mipmap.guide_bg1);
            data.add(R.mipmap.guide_bg2);
            vp_flash.setAdapter(new FlashAdapter(data,this));
        }
    }

    @Override
    public void setListener() {
        animation.setAnimationListener(this);

    }

    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        Version ver = VersionUtil.getVersion("");
        if(ver==null){
            return;
        }
//        String version = ver.getVersion();
        String version = "2";

        int versionCode = 0;
        try {
            versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
            if(versionCode<Integer.parseInt(version)){
                AlertDialog.Builder builder = new AlertDialog.Builder(this)
                        .setMessage("是否升级新版本？")
                        .setNegativeButton("立即升级", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).setPositiveButton("暂不升级", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                iv_flash.setVisibility(View.GONE);
                                vp_flash.setVisibility(View.VISIBLE);
                            }
                        });
                builder.show();

            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
}
